<template>
  <div>
    <el-row type="flex" class="row-bg" justify="space-around">
      <el-col :span="7" v-for="item in appointment">
        <div class="grid-content bg-purple">
          <div>
            <el-avatar :size="50" :src="circleUrl"></el-avatar>
          </div>
          <span>{{item.memberName}}</span><span class="vip">{{level}}</span>
          <div style="overflow: hidden">
            <ul class="left">
              <li>电话号码:</li>
              <li>预约技师:</li>
              <li>预约时间:</li>
            </ul>
            <ul class="right">
              <li>{{item.memberPhoneNumber}}</li>
              <li>{{item.technicianName}}</li>
              <li>{{item.reservationTime}}</li>
            </ul>
          </div>
          <div>
            <div>
              <img src="../../assets/appointment/a.png" alt="">
              <img src="../../assets/appointment/b.png" alt="">
            </div>
            <div>
              <img src="../../assets/appointment/c.png"
                   alt=""
                   @click="dialogFormVisibleChange(item.reservationId,item.reservationState)">
              <img src="../../assets/appointment/d.png"
                   alt=""
                   @click="myDeleteReservation(item.reservationId)">
            </div>
          </div>
        </div>
        <template>
          <el-dialog title="修改预约" :visible.sync="dialogFormVisible">
            <el-form :model="modify">
              <el-form-item label="修改状态" :label-width="formLabelWidth">
                <el-input v-model="modify.state"
                          autocomplete="off"
                          style="width: 221px"></el-input>
              </el-form-item>
              <el-form-item label="修改预约时间" :label-width="formLabelWidth">
                <el-date-picker
                        format="yyyy 年 MM 月 dd 日 HH : mm : ss"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        v-model="modify.reservationTime"
                        type="datetime"
                        placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="myUpdateReservation">确 定</el-button>
            </div>
          </el-dialog>
        </template>
      </el-col>
    </el-row>
    <div>
      <el-pagination
              :current-page="currentPage"
              :page-size="pageSize"
              @current-change="handleCurrentChange"
              prev-text="上一页"
              next-text="下一页"
              layout="prev, pager, next"
              :total='total'>
      </el-pagination>
    </div>
  </div>

</template>

<script>
  import {createNamespacedHelpers} from 'vuex';

  const {mapActions, mapState, mapMutations} = createNamespacedHelpers('appointment');
  export default {
    name: "Appointment",
    data() {
      return {
        circleUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
        name: 'gzz',
        level: '黄金会员',
        date: '',
        dialogFormVisible: false,
        modify: {
          id: '',
          state: '',
          phoneNum: '',
          reservationTime: ''
        },
        formLabelWidth: '160px'
      }
    },
    computed: {
      ...mapState(['appointment', 'total', 'currentPage', 'pageSize', 'pages'])
    },
    methods: {
      ...mapActions(['getAppointment', 'updateReservation', 'deleteReservation']),
      ...mapMutations(['setCurrentPage']),
      handleCurrentChange(val) {
        
        this.setCurrentPage(val)
      },
      dialogFormVisibleChange(id, state) {
        this.dialogFormVisible = true;
        this.modify.id = id;
        this.modify.state = state;
      },
      async myUpdateReservation() {

        const data = await this.updateReservation(this.modify);

        if (data.data.code === 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.getAppointment({reservationState: 1});
        }
        this.dialogFormVisible = false;
      },
      async myDeleteReservation(id) {
        this.$confirm('此操作将永久删除该预约, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteReservation(id);
          this.$message({
            type: 'success',
            message: '删除成功!',
          });
          this.getAppointment({reservationState: 1});
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

        // if (data.data.code === 200) {
        //   this.$message({
        //     message: '删除成功',
        //     type: 'success'
        //   })
        // }
      }
    },
    mounted() {
      this.getAppointment({reservationState: 1});
      console.log(this.appointment);
    },
    watch: {
      currentPage() {
        this.getAppointment({reservationState: 1});
      }
    }

  }
</script>

<style scoped lang="scss">

  img {
    display: inline-block;
    margin: 10px 10px 0;
    width: 80px;
    border-radius: 5px;
  }

  ul {
    display: block;
    list-style: none;
    margin: 10px 0 0;
    padding: 0;
  }

  .left {
    float: left;
    margin-left: 5px;
    color: rgb(142, 142, 142);
  }

  .right {
    float: right;
    margin-right: 5px;
  }

  span {
    font-size: 14px;
  }

  .vip {
    font-size: 12px;
    margin-left: 10px;
    color: gold;
  }

  .el-row {
    margin-bottom: 20px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .el-col {
    border-radius: 4px;
  }

  .bg-purple {
    background: white;
  }

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
    padding: 10px;
  }

  .row-bg {
    padding: 10px 0;
    margin-bottom: 10px;
    /*background-color: #f9fafc;*/
  }
</style>